﻿#include "mythread.h"

myThread::myThread(QTcpSocket *s)
    :mysocket(s)
    ,QObject()
    ,QRunnable()
{
    setAutoDelete(true);
    connect(mysocket, &QTcpSocket::disconnected, this, [this]() {
    emit disconnected(this);
    });
}


void myThread::run()
{


    connect(mysocket, &QTcpSocket::readyRead, this,[=]()
    {
        QByteArray recvdata=mysocket->readAll();
        senddata_to(recvdata);
        emit dataReady(recvdata);
    });//接收数据

    QEventLoop loop;
    connect(mysocket, &QTcpSocket::disconnected, &loop, &QEventLoop::quit);
    loop.exec();

    emit finished(); // 通知线程完成

}

void myThread::senddata_to(QByteArray Data)
{

    QByteArray sendData=Data;
    mysocket->write(sendData);
}
